package com.ruipeng.shengyapo.utils;


import java.security.SecureRandom;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 对象相关的基础工具类
 * @author zhengchb
 */
public class ObjUtils {
	private static Random random = new SecureRandom();
	/**
	 * obj is empty Object
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj){
		return (obj == null) || "".equals(obj.toString().trim());
	}
	
	/**
	 * obj is not empty Object
	 * @param obj
	 * @return
	 */
	public static boolean isNotEmpty(Object obj){
		return !isEmpty(obj);
	}

	/**
	 * obj is an array class
	 * @param obj
	 * @return
	 */
	public static boolean isArray(Object obj) {
		return (obj != null) && (obj.getClass().isArray());
	}

	/**
	 * Object[] is empty
	 * @param array
	 * @return
	 */
	public static boolean isEmpty(Object[] array) {
		return (array == null) || (array.length == 0);
	}
	
	/**
	 * Object[] is empty
	 * @param array
	 * @return
	 */
	public static boolean isEmpty(String[] array) {
		return (array == null) || (array.length == 0);
	}
	
	/**
	 * Object[] is not empty
	 * @param array
	 * @return
	 */
	public static boolean isNotEmpty(Object[] array) {
		return !isEmpty(array);
	}
	
	/**
	 * Object[] is not empty
	 * @param array
	 * @return
	 */
	public static boolean isNotEmpty(String[] array) {
		return !isEmpty(array);
	}

	/**
	 * 判断某对象是否为Boolean且值为真
	 * @param value
	 * @return
	 */
	public static boolean getBoolean(Object value) {
		if (value instanceof Boolean) {
			return ((Boolean) value).booleanValue();
		} else {
			return false;
		}
	}
	
	/**
	 * obj is empty Object return null
	 * @param <T>
	 * @param obj
	 * @return
	 */
	public static <T> T isEmptyReturnNull(T obj){
		if(isEmpty(obj)){
			return null;
		}
		return obj;
	}
	
	/**
	 * obj is empty Object return null
	 * @param <T>
	 * @param obj
	 * @return
	 */
	public static String[] isEmptyReturnNull(String[] obj){
		if(isEmpty(obj)){
			return null;
		}
		return obj;
	}
	public static String randomNumeric(Integer len) {
		return random
				.ints(len, 0, 10)
				.mapToObj(String::valueOf)
				.reduce((s, s1) -> s + s1).get();
	}
	public static String getNowDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}
}
